package com.zbp.utils.jwt;

import com.zbp.model.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class JwtUserUtil {

    /**
     * 获取JwtUser
     *
     * @return JwtUser
     */
    public static UserVO getJwtUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserVO) {
                return (UserVO) principal;
            } else {
                log.info("ATTENTION: this thread has no user information.");
            }
        } else {
            log.info("ATTENTION: this thread has no authentication information.");
        }
        return null;
    }

    /**
     * 获取人员登陆名
     *
     * @return Username
     */
    public static String getUsername() {
        return getJwtUser() != null ? getJwtUser().getUsername() : null;
    }
}
